﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockMonitor.Configuration;

namespace StockMonitor.Forms
{
    public partial class frmEditStockInPool : Form
    {
        static string[] buyPriceType = { "卖一", "卖二", "卖三", "卖四", "卖五", "未设置", "买一", "买二", "买三", "买四", "买五" };
        static string[] sellPriceType = { "买一", "买二", "买三", "买四", "买五", "未设置", "卖一", "卖二", "卖三", "卖四", "卖五" };

        bool IsBuy;
        bool IsLimited;
        StockPoolItem StockItem;
        LimitedStockPoolItem LimitedStockItem;
        Action<StockPoolItem> UpdatePoolUI;
        Action<LimitedStockPoolItem> UpdateLimitedPoolUI;

        public frmEditStockInPool()
        {
            InitializeComponent();

            this.cbStockCode.DataSource = StockListConfig.Instance.AllStock;
            this.cbStockCode.DisplayMember = "StockName";
            this.cbStockCode.ValueMember = "StockCode";
        }

        public frmEditStockInPool(bool isBuy, Action<StockPoolItem> AddToPool)
            : this()
        {
            this.UpdatePoolUI = AddToPool;
            this.IsBuy = isBuy;
            this.IsLimited = false;

            this.lbPrice.Visible = true;
            this.cbPrice.Visible = true;
            this.tbPrice.Visible = false;
            this.lbRMB.Visible = false;
            this.lbNotes.Visible = false;
            this.numAppendAmount.Visible = true;
            this.lbShare.Visible = true;

            this.lbEnabled.Visible = false;
            this.chkEnabled.Visible = false;

            if (isBuy)
            {
                this.lbAmount.Text = "买入数量";
                this.lbPrice.Text = "买入价格";
                this.lbAppendAmount.Text = "加仓数量";
                this.cbPrice.DataSource = buyPriceType;
            }
            else
            {
                this.lbAmount.Text = "卖出数量";
                this.lbPrice.Text = "卖出价格";
                this.lbAppendAmount.Text = "清仓数量";
                this.cbPrice.DataSource = sellPriceType;
            }
        }

        public frmEditStockInPool(bool isBuy, string stockCode, Action<StockPoolItem> EditInPool)
            : this(isBuy, EditInPool)
        {
            StockItem = StockPool.Instance.Pool.Find(s => s.StockCode == stockCode && s.IsBuy == IsBuy);

            if (StockItem != null)
            {
                this.cbStockCode.Text = StockItem.StockCode + "|" + StockItem.StockName;
                this.numAmount.Value = StockItem.ExchangeAmount;
                this.numAppendAmount.Value = StockItem.AppendAmount;
                this.cbStockCode.Enabled = false;
                this.cbPrice.SelectedIndex = 0;
            }
        }

        public frmEditStockInPool(bool isBuy, Action<LimitedStockPoolItem> AddToPool)
            : this()
        {
            this.UpdateLimitedPoolUI = AddToPool;
            this.IsBuy = isBuy;
            this.IsLimited = true;

            this.lbPrice.Visible = false;
            this.cbPrice.Visible = false;
            this.tbPrice.Visible = true;
            this.lbRMB.Visible = true;
            this.lbNotes.Visible = true;
            this.numAppendAmount.Visible = false;
            this.lbShare.Visible = false;
            this.lbEnabled.Visible = true;
            this.chkEnabled.Visible = true;

            if (isBuy)
            {
                this.lbAmount.Text = "买入数量";
                this.lbAppendAmount.Text = "买入限价";
            }
            else
            {
                this.lbAmount.Text = "卖出数量";
                this.lbAppendAmount.Text = "卖出限价";
            }
        }

        public frmEditStockInPool(bool isBuy, string stockCode, Action<LimitedStockPoolItem> EditInPool)
            : this(isBuy, EditInPool)
        {
            LimitedStockItem = LimitedStockPool.Instance.Pool.Find(s => s.StockCode == stockCode && s.IsBuy == IsBuy);

            if (LimitedStockItem != null)
            {
                this.cbStockCode.Text = LimitedStockItem.StockCode + "|" + LimitedStockItem.StockName;
                this.numAmount.Value = LimitedStockItem.ExchangeAmount;
                this.cbStockCode.Enabled = false;
                this.tbPrice.Text = LimitedStockItem.ExchangePrice.ToString();
                this.chkEnabled.Checked = LimitedStockItem.IsEnabled;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if ((numAmount.Value % 100 != 0 || numAmount.Value <= 0) && lbAmount.Text == "买入数量")
            {
                errorProvider1.SetError(numAmount, "买入数量需是100的倍数");
                return;
            }

            if (numAppendAmount.Value % 100 != 0 && lbAppendAmount.Text == "加仓数量")
            {
                errorProvider1.SetError(numAppendAmount, lbAppendAmount.Text + "需是100的倍数");
                return;
            }

            if (!IsLimited)   //自动
            {
                if (StockItem == null)
                {
                    StockItem = new StockPoolItem();
                    StockItem.StockCode = this.cbStockCode.SelectedValue.ToString();
                    StockItem.StockName = this.cbStockCode.Text.Substring(7);

                    if (StockPool.Instance.Pool.Find(s => (s.StockCode == StockItem.StockCode) && (s.IsBuy == this.IsBuy)) != null)
                    {
                        errorProvider1.SetError(cbStockCode, string.Format("此股票已有{0}配置，请修改现有的配置即可", lbAmount.Text.Substring(0, 2)));
                        StockItem = null;
                        return;
                    }

                    StockPool.Instance.Pool.Add(StockItem);
                }

                if (this.cbPrice.SelectedItem.ToString() != "未设置")
                {
                    if (!ValidateConflict(StockItem.StockCode, StockItem.IsBuy, true))
                    {
                        if (MessageBox.Show(StockItem.StockCode + "已在限价交易中配置,您确定要在自动交易中再次配置吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
                            return;
                    }
                }

                StockItem.IsBuy = IsBuy;
                StockItem.ExchangeAmount = Convert.ToInt32(this.numAmount.Value);
                StockItem.AppendAmount = Convert.ToInt32(this.numAppendAmount.Value);

                StockItem.ExchangePriceType = (Enums.PriceType)Enum.Parse(typeof(Enums.PriceType), this.cbPrice.SelectedItem.ToString());



                StockPool.Instance.Save();
                UpdatePoolUI(StockItem);
            }
            else  //限价
            {
                if (LimitedStockItem == null)
                {
                    LimitedStockItem = new LimitedStockPoolItem();
                    LimitedStockItem.StockCode = this.cbStockCode.SelectedValue.ToString();
                    LimitedStockItem.StockName = this.cbStockCode.Text.Substring(7);

                    if (LimitedStockPool.Instance.Pool.Find(s => (s.StockCode == LimitedStockItem.StockCode) && (s.IsBuy == this.IsBuy)) != null)
                    {
                        errorProvider1.SetError(cbStockCode, string.Format("此股票已有{0}配置，请修改现有的配置即可", lbAmount.Text.Substring(0, 2)));
                        LimitedStockItem = null;
                        return;
                    }

                    LimitedStockPool.Instance.Pool.Add(LimitedStockItem);
                }

                if (this.chkEnabled.Checked == true)
                {
                    if (!ValidateConflict(LimitedStockItem.StockCode, LimitedStockItem.IsBuy, false))
                    {
                        if (MessageBox.Show(LimitedStockItem.StockCode + "已在自动交易中配置,您确定要在限价交易中再次配置吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
                            return;
                    }
                }

                LimitedStockItem.IsBuy = IsBuy;
                LimitedStockItem.IsEnabled = this.chkEnabled.Checked;
                LimitedStockItem.ExchangeAmount = Convert.ToInt32(this.numAmount.Value);
                LimitedStockItem.ExchangePrice = this.tbPrice.Text.Trim() == String.Empty ? 0 : Convert.ToDouble(this.tbPrice.Text.Trim());
                
                LimitedStockPool.Instance.Save();
                UpdateLimitedPoolUI(LimitedStockItem);
            }

            this.Close();
        }

        private bool ValidateConflict(string stockCode, bool isBuy, bool isAuto)
        {
            if (isAuto)
            {
                var item = (isBuy ? LimitedStockPool.Instance.BuyPool : LimitedStockPool.Instance.SellPool).Find(s => s.StockCode == stockCode);
                if (item == null || item.IsEnabled == false)  //未配置或未启用
                    return true;
            }
            else
            {
                var item = (isBuy ? StockPool.Instance.BuyPool : StockPool.Instance.SellPool).Find(s => s.StockCode == stockCode);
                if (item == null || item.ExchangePriceType == Enums.PriceType.未设置)  //未配置或未设置
                    return true;
            }

            return false;
        }
    }
}
